﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using System.Web.UI.HtmlControls;
public partial class Page_ContentProduct : SitePage
{
    //------------------------------------------------
    //VARIABLES
    //------------------------------------------------
    public ContentProduct objData = null;
    //------------------------------------------------
    //METHODS
    //------------------------------------------------
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }

    private void LoadData()
    {
        
        string rawUrl = this.Request.RawUrl.Substring(1, this.Request.RawUrl.Length - 1);
        busContentProduct objbus = new busContentProduct();
        objData = objbus.GetByURL(rawUrl, GetCurrentLang());            
        this.SetMetaData(string.Format(Resources.Resource.TITLE, Server.HtmlEncode(objData.Name)), objData.MetaDescription, objData.MetaKeywords);
        Literal ltrBreadcrum = (Literal)this.Master.FindControl("ltrBreadcrum");

        string strPageName = Request.RawUrl.ToString();
        if (strPageName.Contains("/products/"))
        {
            ltrBreadcrum.Text = string.Format(@"<a href=""/products"">{0}</a> {1}", GetLang("Products"), Server.HtmlEncode(objData.Name)); //string.Format(@"" + +"");
        }
        else if (strPageName.Contains("/products"))
        {
            ltrBreadcrum.Text = string.Format(@"<a href=""/products"" class=""last"">{0}</a>", GetLang("Products"));
        }
        else
        {
            ltrBreadcrum.Text = string.Format(@"{0}", Server.HtmlEncode(objData.Name)); 
        }

        try
        {
            if (objData.Content.Contains("[gallery="))
            {
                string strLocation = objData.Content.Substring((objData.Content.IndexOf("[gallery=")) + 9);
                strLocation = strLocation.Substring(0, strLocation.IndexOf("]"));
                objData.Content = objData.Content.Replace("[gallery=" + strLocation + "]", "");

                string strThumbFolder = "/upload/Items/" + strLocation + "/thumb/";
                string strLargeFolder = "/upload/Items/" + strLocation + "/full/";
                strLocation = Server.MapPath(strThumbFolder);
                DirectoryInfo d = new DirectoryInfo(strLocation);
                foreach (FileInfo f in d.GetFiles())
                {
                    litProduct.Text += string.Format(@"<a href=""{2}{3}""><img src=""{0}{1}"" class=""product"" /></a>", strThumbFolder, f.Name, strLargeFolder, f.Name.Replace("_thumb", ""));
                }

            }
        }
        catch (Exception ex)
        {
            litProduct.Text = ex.Message.ToString();
        }
    }
}